package com.arvin.service.serviceImpl;

import com.arvin.mapper.DataCountMapper;
import com.arvin.pojo.DataCount;
import com.arvin.service.DataCountService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

@Service
public class DataCountServiceImpl implements DataCountService {
    @Autowired
    private DataCountMapper dataCountMapper;

    /**
     * 获取报名人员班级分布情况
     * @param className
     * @return
     */
    @Override
    public DataCount getInClassDivideByGrade(String className) {
        ArrayList<String> grades = new ArrayList<>();
        ArrayList<Integer> counts = new ArrayList<>();
        grades.add("大一");
        grades.add("大二");
        grades.add("大三");
        grades.add("大四");
        for (String grade : grades) {
            int count = dataCountMapper.getInClassDivideByGrade(grade,className);
            counts.add(count);
        }
        DataCount dataCount = new DataCount();
        dataCount.setCategory(grades);
        dataCount.setValue(counts);
        return dataCount;
    }

    /**
     * 获取汇总数据
     * @param className
     * @return
     */
    @Override
    public Map getTotalData(String className) {
        Map<String, Integer> result = new HashMap<>();
        //获取总报名人数
        Integer totalApplication = dataCountMapper.getTotalApplication(className);
        //获取入班人数
        Integer totalZYClass = dataCountMapper.getTotalZYClass(className);
        //获取参加笔试人数
        Integer joinExam = dataCountMapper.getJoinExam(className);
        //获取通过笔试人数
        Integer passExam = dataCountMapper.getExamByState("已通过",className);
        //获取参加面试人数
        Integer joinIntv = dataCountMapper.getjoinIntv(className);
        //获取通过面试人数
        Integer passIntv = dataCountMapper.getIntvByState("已通过",className);
        //获取申请免试人数
        Integer askExem = dataCountMapper.getAskExem(className);
        //获取通过申请免试人数
        Integer passExem = dataCountMapper.getExemByState("已通过",className);

        result.put("totalApplication",totalApplication);
        result.put("totalZYClass",totalZYClass);
        result.put("joinExam",joinExam);
        result.put("passExam",passExam);
        result.put("joinIntv",joinIntv);
        result.put("passIntv",passIntv);
        result.put("askExem",askExem);
        result.put("passExem",passExem);
        return result;
    }

    /**
     * 获取报名人员专业分布情况
     * @param className
     * @return
     */
    @Override
    public DataCount getInClassDivideBySpecial(String className) {
        HashSet<String> specialList = dataCountMapper.getAllStudentSpecialty(className);
        List<String> category = new ArrayList<>();
        List<Integer> value = new ArrayList<>();
        for (String special : specialList) {
            Integer count = dataCountMapper.getDivideBySpecial(special,className);
            if (count != null && count != 0){
                category.add(special);
                value.add(count);
            }
        }
        return new DataCount(category,value);
    }

    /**
     * 获取分数段人数
     * @param tableName
     * @param className
     * @return
     */
    @Override
    public DataCount getScoreCount(String tableName, String className) {
        List<String> category = new ArrayList<>();
        List<Integer> value = new ArrayList<>();
        Integer count = 0;
        if ("t_exam".equals(tableName)){
            count = dataCountMapper.getExamScoreCount(className,0,59);
            category.add("60分以下");value.add(count);
            count = dataCountMapper.getExamScoreCount(className,60,69);
            category.add("60分-69分");value.add(count);
            count = dataCountMapper.getExamScoreCount(className,70,79);
            category.add("70分-79分");value.add(count);
            count = dataCountMapper.getExamScoreCount(className,80,89);
            category.add("80分-89分");value.add(count);
            count = dataCountMapper.getExamScoreCount(className,90,100);
            category.add("90分-100分");value.add(count);
        } else if ("t_interview".equals(tableName)) {
            count = dataCountMapper.getIntvScoreCount(className,0,59);
            category.add("60分以下");value.add(count);
            count = dataCountMapper.getIntvScoreCount(className,60,69);
            category.add("60分-69分");value.add(count);
            count = dataCountMapper.getIntvScoreCount(className,70,79);
            category.add("70分-79分");value.add(count);
            count = dataCountMapper.getIntvScoreCount(className,80,89);
            category.add("80分-89分");value.add(count);
            count = dataCountMapper.getIntvScoreCount(className,90,100);
            category.add("90分-100分");value.add(count);
        }
        return new DataCount(category,value);
    }

    /**
     * 获取考试状态分布信息
     * @param className
     * @return
     */
    @Override
    public DataCount getExamStateCount(String className) {
        List<String> state = new ArrayList<>();
        List<String> category = new ArrayList<>();
        List<Integer> value = new ArrayList<>();
        state.addAll(Arrays.asList("未考试","已考试","未通过","已通过"));
        for (String stateItem : state) {
            Integer count = dataCountMapper.getExamStateCount(className,stateItem);
            if (count != null && count != 0){
                category.add(stateItem);
                value.add(count);
            }
        }
        return new DataCount(category,value);
    }

    /**
     * 获取面试状态分布信息
     * @param className
     * @return
     */
    @Override
    public DataCount getIntvStateCount(String className) {
        List<String> state = new ArrayList<>();
        List<String> category = new ArrayList<>();
        List<Integer> value = new ArrayList<>();
        state.addAll(Arrays.asList("未面试","已面试","未通过","已通过"));
        for (String stateItem : state) {
            Integer count = dataCountMapper.getIntvStateCount(className,stateItem);
            if (count != null && count != 0){
                category.add(stateItem);
                value.add(count);
            }
        }
        return new DataCount(category,value);
    }
    /**
     * 获取所有卓越班级列表信息
     * @return
     */
    @Override
    public HashSet<String> getAllClassName() {
        HashSet<String> className = dataCountMapper.getAllClassName();
        HashSet<String> result = new HashSet<>();
        for (String s : className) {
            if (s != null && s != ""){
                result.add(s);
            }
        }
        return result;
    }
    /**
     * 获取入班人员班级分布信息
     * @param className
     * @return
     */
    @Override
    public DataCount getInClassStudentClass(String className) {
        HashSet<String> studentClassName = dataCountMapper.getInClassStudentClass(className);
        ArrayList<String> category = new ArrayList<>();
        ArrayList<Integer> value = new ArrayList<>();
        for (String stuClassName : studentClassName) {
            Integer num = dataCountMapper.getNumByStudentClassName(className,stuClassName);
            if (num != null && num != 0){
                category.add(stuClassName);
                value.add(num);
            }
        }
        return new DataCount(category,value);
    }
}
